#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: views_filters.py
@contact: zwb15083976291@163.com
@time: 2021/11/21
"""


class BaseSearchFilter(object):
    __search_field__ = ''
    __query_data_field__ = "search"

    def filter_queryset(self, request, query_set, view):
        validated_data = view.serializer.validated_data
        if validated_data.get(self.__query_data_field__, None):
            search_data = validated_data.get(self.__query_data_field__).strip()
            query_set = query_set.filter(**{self.__search_field__: search_data})
        return query_set


class SearchTagsFilter(BaseSearchFilter):
    __search_field__ = 'tag_name__contains'


class SearchCategoriesFilter(BaseSearchFilter):
    __search_field__ = 'categorie_name__contains'


class SearchLinksFilter(BaseSearchFilter):
    __search_field__ = 'link_name__contains'


class SearchMusicsFilter(BaseSearchFilter):
    __search_field__ = 'music_name__contains'


class SearchPagesFilter(BaseSearchFilter):
    __search_field__ = 'page_label__contains'
    __query_data_field__ = 'page_label'


# 后台草稿过滤
class SearchDraftFilter(BaseSearchFilter):
    __search_field__ = 'draft_title__contains'


# 后台文章条件过滤
class SearchArticlesFilter(BaseSearchFilter):
    __search_field__ = 'articles_title__contains'

    # 其他过滤条件
    __other_field__ = ['categorie', 'articles_type', 'articles_status']

    def filter_queryset(self, request, query_set, view):
        validated_data = view.serializer.validated_data
        query_set = super().filter_queryset(request, query_set, view)
        # 其他过滤条件
        for field in self.__other_field__:
            if validated_data.get(field, None) is not None:
                search_data = str(validated_data.get(field)).strip()
                query_set = query_set.filter(**{field: search_data})
        return query_set

# 后台评论搜索过滤
# 后台草稿过滤
class SearchCommentsFilter(BaseSearchFilter):
    __search_field__ = 'name__contains'
